3 Dimensional Array Manipulations and Equalization


In [2]:
### Run below if necessary

##import sys

##sys.path.append('/usr/local/lib/python2.7/site-packages')

import math
import csv,gc
import matplotlib
import numpy as np
import cv2

#%matplotlib
BINS = 32

In [3]:
import matplotlib.pyplot as plt
%matplotlib inline

In [4]:
from skimage import data, img_as_float
from skimage import exposure

In [5]:
b = np.random.randint(0,10,(2,3,4)) 
print b


[[[0 4 6 6]
  [4 6 6 0]
  [8 6 9 6]]

 [[1 6 8 3]
  [0 6 6 4]
  [3 5 9 5]]]

In [6]:
print b[0]
print b[1]
print "---------"
print b[0,0]
print b[0,1]


[[0 4 6 6]
 [4 6 6 0]
 [8 6 9 6]]
[[1 6 8 3]
 [0 6 6 4]
 [3 5 9 5]]
---------
[0 4 6 6]
[4 6 6 0]

In [7]:
bb = b.reshape(-1)
print bb
print bb.sum()


[0 4 6 6 4 6 6 0 8 6 9 6 1 6 8 3 0 6 6 4 3 5 9 5]
117

In [32]:
plt.hist(bb)
plt.show()



In [8]:
bhistnorm = bb*1.0/sum(bb)
print bhistnorm


[ 0.          0.03418803  0.05128205  0.05128205  0.03418803  0.05128205
  0.05128205  0.          0.06837607  0.05128205  0.07692308  0.05128205
  0.00854701  0.05128205  0.06837607  0.02564103  0.          0.05128205
  0.05128205  0.03418803  0.02564103  0.04273504  0.07692308  0.04273504]

In [10]:
plt.hist(bhistnorm, bins = 10)
plt.show

## Histogram looks different than above due to the relatively large histogram sum number


Out[10]:
<function matplotlib.pyplot.show>

In [16]:
a = np.random.randint(0,10,(6,7,8)) 
print a


[[[8 2 7 3 3 8 0 1]
  [9 4 9 6 7 2 1 8]
  [7 7 2 7 4 6 1 5]
  [4 4 7 4 6 4 9 2]
  [4 8 8 6 7 3 4 0]
  [6 7 3 2 4 3 7 3]
  [4 5 4 3 0 3 4 9]]

 [[6 7 4 8 9 0 6 5]
  [4 9 6 7 5 3 5 8]
  [9 6 3 7 7 3 3 3]
  [3 5 0 5 9 3 7 3]
  [4 8 2 9 6 3 2 4]
  [3 2 6 1 3 7 1 5]
  [8 8 3 0 3 7 2 4]]

 [[6 1 5 2 7 7 4 3]
  [2 0 0 7 6 8 4 7]
  [3 4 1 8 7 1 1 5]
  [8 9 3 1 7 1 6 5]
  [2 3 2 4 8 7 7 8]
  [2 8 0 5 9 5 6 0]
  [2 0 1 9 7 1 7 9]]

 [[9 6 8 0 8 2 7 9]
  [3 9 0 7 6 5 3 3]
  [0 7 4 7 4 8 2 2]
  [4 8 0 5 1 8 7 5]
  [9 1 5 1 2 2 5 9]
  [6 5 3 4 2 2 5 0]
  [3 5 4 1 0 6 4 6]]

 [[7 2 2 2 1 1 9 4]
  [9 8 9 5 9 9 5 3]
  [6 6 3 0 3 8 3 3]
  [0 6 7 3 9 4 2 4]
  [1 1 3 3 2 2 4 2]
  [9 5 7 3 2 4 3 9]
  [1 4 6 2 7 2 9 9]]

 [[4 1 9 2 8 7 8 8]
  [6 9 2 8 6 8 7 4]
  [6 4 7 5 4 5 1 4]
  [4 6 0 7 1 7 9 5]
  [9 6 9 6 1 3 7 6]
  [2 1 7 7 5 0 7 9]
  [9 5 4 9 4 5 9 3]]]

In [17]:
print a[0]
print a[1]


[[8 2 7 3 3 8 0 1]
 [9 4 9 6 7 2 1 8]
 [7 7 2 7 4 6 1 5]
 [4 4 7 4 6 4 9 2]
 [4 8 8 6 7 3 4 0]
 [6 7 3 2 4 3 7 3]
 [4 5 4 3 0 3 4 9]]
[[6 7 4 8 9 0 6 5]
 [4 9 6 7 5 3 5 8]
 [9 6 3 7 7 3 3 3]
 [3 5 0 5 9 3 7 3]
 [4 8 2 9 6 3 2 4]
 [3 2 6 1 3 7 1 5]
 [8 8 3 0 3 7 2 4]]

In [18]:
print len(a) 
print len(a[0]) 
print len(a[0][0])

i=0
for row in img:
    i+=1
print i

j=0
for row in img:
    for row in row:
        j+=1
print j


6
7
8
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-18-1a055888a303> in <module>()
      4 
      5 i=0
----> 6 for row in img:
      7     i+=1
      8 print i

NameError: name 'img' is not defined

In [19]:
aa = a.reshape(-1)
print aa
print aa.sum()


[8 2 7 3 3 8 0 1 9 4 9 6 7 2 1 8 7 7 2 7 4 6 1 5 4 4 7 4 6 4 9 2 4 8 8 6 7
 3 4 0 6 7 3 2 4 3 7 3 4 5 4 3 0 3 4 9 6 7 4 8 9 0 6 5 4 9 6 7 5 3 5 8 9 6
 3 7 7 3 3 3 3 5 0 5 9 3 7 3 4 8 2 9 6 3 2 4 3 2 6 1 3 7 1 5 8 8 3 0 3 7 2
 4 6 1 5 2 7 7 4 3 2 0 0 7 6 8 4 7 3 4 1 8 7 1 1 5 8 9 3 1 7 1 6 5 2 3 2 4
 8 7 7 8 2 8 0 5 9 5 6 0 2 0 1 9 7 1 7 9 9 6 8 0 8 2 7 9 3 9 0 7 6 5 3 3 0
 7 4 7 4 8 2 2 4 8 0 5 1 8 7 5 9 1 5 1 2 2 5 9 6 5 3 4 2 2 5 0 3 5 4 1 0 6
 4 6 7 2 2 2 1 1 9 4 9 8 9 5 9 9 5 3 6 6 3 0 3 8 3 3 0 6 7 3 9 4 2 4 1 1 3
 3 2 2 4 2 9 5 7 3 2 4 3 9 1 4 6 2 7 2 9 9 4 1 9 2 8 7 8 8 6 9 2 8 6 8 7 4
 6 4 7 5 4 5 1 4 4 6 0 7 1 7 9 5 9 6 9 6 1 3 7 6 2 1 7 7 5 0 7 9 9 5 4 9 4
 5 9 3]
1588

In [20]:
plt.hist(aa)
plt.show()



In [21]:
ahistnorm = aa*1.0/sum(aa)
print ahistnorm


[ 0.00503778  0.00125945  0.00440806  0.00188917  0.00188917  0.00503778
  0.          0.00062972  0.00566751  0.00251889  0.00566751  0.00377834
  0.00440806  0.00125945  0.00062972  0.00503778  0.00440806  0.00440806
  0.00125945  0.00440806  0.00251889  0.00377834  0.00062972  0.00314861
  0.00251889  0.00251889  0.00440806  0.00251889  0.00377834  0.00251889
  0.00566751  0.00125945  0.00251889  0.00503778  0.00503778  0.00377834
  0.00440806  0.00188917  0.00251889  0.          0.00377834  0.00440806
  0.00188917  0.00125945  0.00251889  0.00188917  0.00440806  0.00188917
  0.00251889  0.00314861  0.00251889  0.00188917  0.          0.00188917
  0.00251889  0.00566751  0.00377834  0.00440806  0.00251889  0.00503778
  0.00566751  0.          0.00377834  0.00314861  0.00251889  0.00566751
  0.00377834  0.00440806  0.00314861  0.00188917  0.00314861  0.00503778
  0.00566751  0.00377834  0.00188917  0.00440806  0.00440806  0.00188917
  0.00188917  0.00188917  0.00188917  0.00314861  0.          0.00314861
  0.00566751  0.00188917  0.00440806  0.00188917  0.00251889  0.00503778
  0.00125945  0.00566751  0.00377834  0.00188917  0.00125945  0.00251889
  0.00188917  0.00125945  0.00377834  0.00062972  0.00188917  0.00440806
  0.00062972  0.00314861  0.00503778  0.00503778  0.00188917  0.
  0.00188917  0.00440806  0.00125945  0.00251889  0.00377834  0.00062972
  0.00314861  0.00125945  0.00440806  0.00440806  0.00251889  0.00188917
  0.00125945  0.          0.          0.00440806  0.00377834  0.00503778
  0.00251889  0.00440806  0.00188917  0.00251889  0.00062972  0.00503778
  0.00440806  0.00062972  0.00062972  0.00314861  0.00503778  0.00566751
  0.00188917  0.00062972  0.00440806  0.00062972  0.00377834  0.00314861
  0.00125945  0.00188917  0.00125945  0.00251889  0.00503778  0.00440806
  0.00440806  0.00503778  0.00125945  0.00503778  0.          0.00314861
  0.00566751  0.00314861  0.00377834  0.          0.00125945  0.
  0.00062972  0.00566751  0.00440806  0.00062972  0.00440806  0.00566751
  0.00566751  0.00377834  0.00503778  0.          0.00503778  0.00125945
  0.00440806  0.00566751  0.00188917  0.00566751  0.          0.00440806
  0.00377834  0.00314861  0.00188917  0.00188917  0.          0.00440806
  0.00251889  0.00440806  0.00251889  0.00503778  0.00125945  0.00125945
  0.00251889  0.00503778  0.          0.00314861  0.00062972  0.00503778
  0.00440806  0.00314861  0.00566751  0.00062972  0.00314861  0.00062972
  0.00125945  0.00125945  0.00314861  0.00566751  0.00377834  0.00314861
  0.00188917  0.00251889  0.00125945  0.00125945  0.00314861  0.
  0.00188917  0.00314861  0.00251889  0.00062972  0.          0.00377834
  0.00251889  0.00377834  0.00440806  0.00125945  0.00125945  0.00125945
  0.00062972  0.00062972  0.00566751  0.00251889  0.00566751  0.00503778
  0.00566751  0.00314861  0.00566751  0.00566751  0.00314861  0.00188917
  0.00377834  0.00377834  0.00188917  0.          0.00188917  0.00503778
  0.00188917  0.00188917  0.          0.00377834  0.00440806  0.00188917
  0.00566751  0.00251889  0.00125945  0.00251889  0.00062972  0.00062972
  0.00188917  0.00188917  0.00125945  0.00125945  0.00251889  0.00125945
  0.00566751  0.00314861  0.00440806  0.00188917  0.00125945  0.00251889
  0.00188917  0.00566751  0.00062972  0.00251889  0.00377834  0.00125945
  0.00440806  0.00125945  0.00566751  0.00566751  0.00251889  0.00062972
  0.00566751  0.00125945  0.00503778  0.00440806  0.00503778  0.00503778
  0.00377834  0.00566751  0.00125945  0.00503778  0.00377834  0.00503778
  0.00440806  0.00251889  0.00377834  0.00251889  0.00440806  0.00314861
  0.00251889  0.00314861  0.00062972  0.00251889  0.00251889  0.00377834
  0.          0.00440806  0.00062972  0.00440806  0.00566751  0.00314861
  0.00566751  0.00377834  0.00566751  0.00377834  0.00062972  0.00188917
  0.00440806  0.00377834  0.00125945  0.00062972  0.00440806  0.00440806
  0.00314861  0.          0.00440806  0.00566751  0.00566751  0.00314861
  0.00251889  0.00566751  0.00251889  0.00314861  0.00566751  0.00188917]

In [22]:
plt.hist(ahistnorm, bins = 10)
plt.show


Out[22]:
<function matplotlib.pyplot.show>

Going back to the source code to perform histogram equalization


In [27]:
img = a

print img

print " "

print img[0]
print img[1]

print " "

imgflat = img.reshape(-1)
print imgflat
print imgflat.sum()

print " "
fig = plt.hist(imgflat, bins='auto')
plt.title('Histogram')
plt.show()

print " "

imgnorm = imgflat*1.0/sum(imgflat)
print imgnorm

fig = plt.hist(imgnorm, bins = 'auto')
plt.title('Normalized Histogram')
plt.show


[[[8 2 7 3 3 8 0 1]
  [9 4 9 6 7 2 1 8]
  [7 7 2 7 4 6 1 5]
  [4 4 7 4 6 4 9 2]
  [4 8 8 6 7 3 4 0]
  [6 7 3 2 4 3 7 3]
  [4 5 4 3 0 3 4 9]]

 [[6 7 4 8 9 0 6 5]
  [4 9 6 7 5 3 5 8]
  [9 6 3 7 7 3 3 3]
  [3 5 0 5 9 3 7 3]
  [4 8 2 9 6 3 2 4]
  [3 2 6 1 3 7 1 5]
  [8 8 3 0 3 7 2 4]]

 [[6 1 5 2 7 7 4 3]
  [2 0 0 7 6 8 4 7]
  [3 4 1 8 7 1 1 5]
  [8 9 3 1 7 1 6 5]
  [2 3 2 4 8 7 7 8]
  [2 8 0 5 9 5 6 0]
  [2 0 1 9 7 1 7 9]]

 [[9 6 8 0 8 2 7 9]
  [3 9 0 7 6 5 3 3]
  [0 7 4 7 4 8 2 2]
  [4 8 0 5 1 8 7 5]
  [9 1 5 1 2 2 5 9]
  [6 5 3 4 2 2 5 0]
  [3 5 4 1 0 6 4 6]]

 [[7 2 2 2 1 1 9 4]
  [9 8 9 5 9 9 5 3]
  [6 6 3 0 3 8 3 3]
  [0 6 7 3 9 4 2 4]
  [1 1 3 3 2 2 4 2]
  [9 5 7 3 2 4 3 9]
  [1 4 6 2 7 2 9 9]]

 [[4 1 9 2 8 7 8 8]
  [6 9 2 8 6 8 7 4]
  [6 4 7 5 4 5 1 4]
  [4 6 0 7 1 7 9 5]
  [9 6 9 6 1 3 7 6]
  [2 1 7 7 5 0 7 9]
  [9 5 4 9 4 5 9 3]]]
 
[[8 2 7 3 3 8 0 1]
 [9 4 9 6 7 2 1 8]
 [7 7 2 7 4 6 1 5]
 [4 4 7 4 6 4 9 2]
 [4 8 8 6 7 3 4 0]
 [6 7 3 2 4 3 7 3]
 [4 5 4 3 0 3 4 9]]
[[6 7 4 8 9 0 6 5]
 [4 9 6 7 5 3 5 8]
 [9 6 3 7 7 3 3 3]
 [3 5 0 5 9 3 7 3]
 [4 8 2 9 6 3 2 4]
 [3 2 6 1 3 7 1 5]
 [8 8 3 0 3 7 2 4]]
 
[8 2 7 3 3 8 0 1 9 4 9 6 7 2 1 8 7 7 2 7 4 6 1 5 4 4 7 4 6 4 9 2 4 8 8 6 7
 3 4 0 6 7 3 2 4 3 7 3 4 5 4 3 0 3 4 9 6 7 4 8 9 0 6 5 4 9 6 7 5 3 5 8 9 6
 3 7 7 3 3 3 3 5 0 5 9 3 7 3 4 8 2 9 6 3 2 4 3 2 6 1 3 7 1 5 8 8 3 0 3 7 2
 4 6 1 5 2 7 7 4 3 2 0 0 7 6 8 4 7 3 4 1 8 7 1 1 5 8 9 3 1 7 1 6 5 2 3 2 4
 8 7 7 8 2 8 0 5 9 5 6 0 2 0 1 9 7 1 7 9 9 6 8 0 8 2 7 9 3 9 0 7 6 5 3 3 0
 7 4 7 4 8 2 2 4 8 0 5 1 8 7 5 9 1 5 1 2 2 5 9 6 5 3 4 2 2 5 0 3 5 4 1 0 6
 4 6 7 2 2 2 1 1 9 4 9 8 9 5 9 9 5 3 6 6 3 0 3 8 3 3 0 6 7 3 9 4 2 4 1 1 3
 3 2 2 4 2 9 5 7 3 2 4 3 9 1 4 6 2 7 2 9 9 4 1 9 2 8 7 8 8 6 9 2 8 6 8 7 4
 6 4 7 5 4 5 1 4 4 6 0 7 1 7 9 5 9 6 9 6 1 3 7 6 2 1 7 7 5 0 7 9 9 5 4 9 4
 5 9 3]
1588
 
 
[ 0.00503778  0.00125945  0.00440806  0.00188917  0.00188917  0.00503778
  0.          0.00062972  0.00566751  0.00251889  0.00566751  0.00377834
  0.00440806  0.00125945  0.00062972  0.00503778  0.00440806  0.00440806
  0.00125945  0.00440806  0.00251889  0.00377834  0.00062972  0.00314861
  0.00251889  0.00251889  0.00440806  0.00251889  0.00377834  0.00251889
  0.00566751  0.00125945  0.00251889  0.00503778  0.00503778  0.00377834
  0.00440806  0.00188917  0.00251889  0.          0.00377834  0.00440806
  0.00188917  0.00125945  0.00251889  0.00188917  0.00440806  0.00188917
  0.00251889  0.00314861  0.00251889  0.00188917  0.          0.00188917
  0.00251889  0.00566751  0.00377834  0.00440806  0.00251889  0.00503778
  0.00566751  0.          0.00377834  0.00314861  0.00251889  0.00566751
  0.00377834  0.00440806  0.00314861  0.00188917  0.00314861  0.00503778
  0.00566751  0.00377834  0.00188917  0.00440806  0.00440806  0.00188917
  0.00188917  0.00188917  0.00188917  0.00314861  0.          0.00314861
  0.00566751  0.00188917  0.00440806  0.00188917  0.00251889  0.00503778
  0.00125945  0.00566751  0.00377834  0.00188917  0.00125945  0.00251889
  0.00188917  0.00125945  0.00377834  0.00062972  0.00188917  0.00440806
  0.00062972  0.00314861  0.00503778  0.00503778  0.00188917  0.
  0.00188917  0.00440806  0.00125945  0.00251889  0.00377834  0.00062972
  0.00314861  0.00125945  0.00440806  0.00440806  0.00251889  0.00188917
  0.00125945  0.          0.          0.00440806  0.00377834  0.00503778
  0.00251889  0.00440806  0.00188917  0.00251889  0.00062972  0.00503778
  0.00440806  0.00062972  0.00062972  0.00314861  0.00503778  0.00566751
  0.00188917  0.00062972  0.00440806  0.00062972  0.00377834  0.00314861
  0.00125945  0.00188917  0.00125945  0.00251889  0.00503778  0.00440806
  0.00440806  0.00503778  0.00125945  0.00503778  0.          0.00314861
  0.00566751  0.00314861  0.00377834  0.          0.00125945  0.
  0.00062972  0.00566751  0.00440806  0.00062972  0.00440806  0.00566751
  0.00566751  0.00377834  0.00503778  0.          0.00503778  0.00125945
  0.00440806  0.00566751  0.00188917  0.00566751  0.          0.00440806
  0.00377834  0.00314861  0.00188917  0.00188917  0.          0.00440806
  0.00251889  0.00440806  0.00251889  0.00503778  0.00125945  0.00125945
  0.00251889  0.00503778  0.          0.00314861  0.00062972  0.00503778
  0.00440806  0.00314861  0.00566751  0.00062972  0.00314861  0.00062972
  0.00125945  0.00125945  0.00314861  0.00566751  0.00377834  0.00314861
  0.00188917  0.00251889  0.00125945  0.00125945  0.00314861  0.
  0.00188917  0.00314861  0.00251889  0.00062972  0.          0.00377834
  0.00251889  0.00377834  0.00440806  0.00125945  0.00125945  0.00125945
  0.00062972  0.00062972  0.00566751  0.00251889  0.00566751  0.00503778
  0.00566751  0.00314861  0.00566751  0.00566751  0.00314861  0.00188917
  0.00377834  0.00377834  0.00188917  0.          0.00188917  0.00503778
  0.00188917  0.00188917  0.          0.00377834  0.00440806  0.00188917
  0.00566751  0.00251889  0.00125945  0.00251889  0.00062972  0.00062972
  0.00188917  0.00188917  0.00125945  0.00125945  0.00251889  0.00125945
  0.00566751  0.00314861  0.00440806  0.00188917  0.00125945  0.00251889
  0.00188917  0.00566751  0.00062972  0.00251889  0.00377834  0.00125945
  0.00440806  0.00125945  0.00566751  0.00566751  0.00251889  0.00062972
  0.00566751  0.00125945  0.00503778  0.00440806  0.00503778  0.00503778
  0.00377834  0.00566751  0.00125945  0.00503778  0.00377834  0.00503778
  0.00440806  0.00251889  0.00377834  0.00251889  0.00440806  0.00314861
  0.00251889  0.00314861  0.00062972  0.00251889  0.00251889  0.00377834
  0.          0.00440806  0.00062972  0.00440806  0.00566751  0.00314861
  0.00566751  0.00377834  0.00566751  0.00377834  0.00062972  0.00188917
  0.00440806  0.00377834  0.00125945  0.00062972  0.00440806  0.00440806
  0.00314861  0.          0.00440806  0.00566751  0.00566751  0.00314861
  0.00251889  0.00566751  0.00251889  0.00314861  0.00566751  0.00188917]
Out[27]:
<function matplotlib.pyplot.show>

In [28]:
import math

## Reminder of Z's values
print img

# Number of pixels
pixels = len(img) * len(img[0]) * len(img[0][0])
print "The number of pixels is {}".format(pixels)
## The output should be 4 since there are 4 numbers in this array

##Flatten method doesn't work because of tuple conversion later on
'''# Now we flatten Z
zz = z.reshape(-1)'''


# Initialize histogram and CDF
hist = {}
cdf = {}
norm_cdf = {}
## The range value should be be adjusted to the bin number
### In our case the range and bin number is obviously 10 
### since we generated numbers 1-10
BINS = 255

for i in range(BINS):
    hist[i] = 0
    cdf[i] = 0
    norm_cdf[i] = 0

# Create histogram


for row in img:
    for row in row:
        for val in row:
            hist[val] += 1

'''
for val in zz:
    hist[val] += 1
'''    


# Create cdf
for i in range(BINS):
    for j in range(i+1):
        cdf[i] += hist[j]
    norm_cdf[i] = int(math.floor(float(cdf[i]-1)/63*BINS))
    
print "The histogram values are {}".format(hist)
print "The cdf values are {}".format(cdf)
print "The normalized cdf values are {}".format(norm_cdf)


[[[8 2 7 3 3 8 0 1]
  [9 4 9 6 7 2 1 8]
  [7 7 2 7 4 6 1 5]
  [4 4 7 4 6 4 9 2]
  [4 8 8 6 7 3 4 0]
  [6 7 3 2 4 3 7 3]
  [4 5 4 3 0 3 4 9]]

 [[6 7 4 8 9 0 6 5]
  [4 9 6 7 5 3 5 8]
  [9 6 3 7 7 3 3 3]
  [3 5 0 5 9 3 7 3]
  [4 8 2 9 6 3 2 4]
  [3 2 6 1 3 7 1 5]
  [8 8 3 0 3 7 2 4]]

 [[6 1 5 2 7 7 4 3]
  [2 0 0 7 6 8 4 7]
  [3 4 1 8 7 1 1 5]
  [8 9 3 1 7 1 6 5]
  [2 3 2 4 8 7 7 8]
  [2 8 0 5 9 5 6 0]
  [2 0 1 9 7 1 7 9]]

 [[9 6 8 0 8 2 7 9]
  [3 9 0 7 6 5 3 3]
  [0 7 4 7 4 8 2 2]
  [4 8 0 5 1 8 7 5]
  [9 1 5 1 2 2 5 9]
  [6 5 3 4 2 2 5 0]
  [3 5 4 1 0 6 4 6]]

 [[7 2 2 2 1 1 9 4]
  [9 8 9 5 9 9 5 3]
  [6 6 3 0 3 8 3 3]
  [0 6 7 3 9 4 2 4]
  [1 1 3 3 2 2 4 2]
  [9 5 7 3 2 4 3 9]
  [1 4 6 2 7 2 9 9]]

 [[4 1 9 2 8 7 8 8]
  [6 9 2 8 6 8 7 4]
  [6 4 7 5 4 5 1 4]
  [4 6 0 7 1 7 9 5]
  [9 6 9 6 1 3 7 6]
  [2 1 7 7 5 0 7 9]
  [9 5 4 9 4 5 9 3]]]
The number of pixels is 336
The histogram values are {0: 21, 1: 27, 2: 35, 3: 42, 4: 41, 5: 30, 6: 31, 7: 44, 8: 28, 9: 37, 10: 0, 11: 0, 12: 0, 13: 0, 14: 0, 15: 0, 16: 0, 17: 0, 18: 0, 19: 0, 20: 0, 21: 0, 22: 0, 23: 0, 24: 0, 25: 0, 26: 0, 27: 0, 28: 0, 29: 0, 30: 0, 31: 0, 32: 0, 33: 0, 34: 0, 35: 0, 36: 0, 37: 0, 38: 0, 39: 0, 40: 0, 41: 0, 42: 0, 43: 0, 44: 0, 45: 0, 46: 0, 47: 0, 48: 0, 49: 0, 50: 0, 51: 0, 52: 0, 53: 0, 54: 0, 55: 0, 56: 0, 57: 0, 58: 0, 59: 0, 60: 0, 61: 0, 62: 0, 63: 0, 64: 0, 65: 0, 66: 0, 67: 0, 68: 0, 69: 0, 70: 0, 71: 0, 72: 0, 73: 0, 74: 0, 75: 0, 76: 0, 77: 0, 78: 0, 79: 0, 80: 0, 81: 0, 82: 0, 83: 0, 84: 0, 85: 0, 86: 0, 87: 0, 88: 0, 89: 0, 90: 0, 91: 0, 92: 0, 93: 0, 94: 0, 95: 0, 96: 0, 97: 0, 98: 0, 99: 0, 100: 0, 101: 0, 102: 0, 103: 0, 104: 0, 105: 0, 106: 0, 107: 0, 108: 0, 109: 0, 110: 0, 111: 0, 112: 0, 113: 0, 114: 0, 115: 0, 116: 0, 117: 0, 118: 0, 119: 0, 120: 0, 121: 0, 122: 0, 123: 0, 124: 0, 125: 0, 126: 0, 127: 0, 128: 0, 129: 0, 130: 0, 131: 0, 132: 0, 133: 0, 134: 0, 135: 0, 136: 0, 137: 0, 138: 0, 139: 0, 140: 0, 141: 0, 142: 0, 143: 0, 144: 0, 145: 0, 146: 0, 147: 0, 148: 0, 149: 0, 150: 0, 151: 0, 152: 0, 153: 0, 154: 0, 155: 0, 156: 0, 157: 0, 158: 0, 159: 0, 160: 0, 161: 0, 162: 0, 163: 0, 164: 0, 165: 0, 166: 0, 167: 0, 168: 0, 169: 0, 170: 0, 171: 0, 172: 0, 173: 0, 174: 0, 175: 0, 176: 0, 177: 0, 178: 0, 179: 0, 180: 0, 181: 0, 182: 0, 183: 0, 184: 0, 185: 0, 186: 0, 187: 0, 188: 0, 189: 0, 190: 0, 191: 0, 192: 0, 193: 0, 194: 0, 195: 0, 196: 0, 197: 0, 198: 0, 199: 0, 200: 0, 201: 0, 202: 0, 203: 0, 204: 0, 205: 0, 206: 0, 207: 0, 208: 0, 209: 0, 210: 0, 211: 0, 212: 0, 213: 0, 214: 0, 215: 0, 216: 0, 217: 0, 218: 0, 219: 0, 220: 0, 221: 0, 222: 0, 223: 0, 224: 0, 225: 0, 226: 0, 227: 0, 228: 0, 229: 0, 230: 0, 231: 0, 232: 0, 233: 0, 234: 0, 235: 0, 236: 0, 237: 0, 238: 0, 239: 0, 240: 0, 241: 0, 242: 0, 243: 0, 244: 0, 245: 0, 246: 0, 247: 0, 248: 0, 249: 0, 250: 0, 251: 0, 252: 0, 253: 0, 254: 0}
The cdf values are {0: 21, 1: 48, 2: 83, 3: 125, 4: 166, 5: 196, 6: 227, 7: 271, 8: 299, 9: 336, 10: 336, 11: 336, 12: 336, 13: 336, 14: 336, 15: 336, 16: 336, 17: 336, 18: 336, 19: 336, 20: 336, 21: 336, 22: 336, 23: 336, 24: 336, 25: 336, 26: 336, 27: 336, 28: 336, 29: 336, 30: 336, 31: 336, 32: 336, 33: 336, 34: 336, 35: 336, 36: 336, 37: 336, 38: 336, 39: 336, 40: 336, 41: 336, 42: 336, 43: 336, 44: 336, 45: 336, 46: 336, 47: 336, 48: 336, 49: 336, 50: 336, 51: 336, 52: 336, 53: 336, 54: 336, 55: 336, 56: 336, 57: 336, 58: 336, 59: 336, 60: 336, 61: 336, 62: 336, 63: 336, 64: 336, 65: 336, 66: 336, 67: 336, 68: 336, 69: 336, 70: 336, 71: 336, 72: 336, 73: 336, 74: 336, 75: 336, 76: 336, 77: 336, 78: 336, 79: 336, 80: 336, 81: 336, 82: 336, 83: 336, 84: 336, 85: 336, 86: 336, 87: 336, 88: 336, 89: 336, 90: 336, 91: 336, 92: 336, 93: 336, 94: 336, 95: 336, 96: 336, 97: 336, 98: 336, 99: 336, 100: 336, 101: 336, 102: 336, 103: 336, 104: 336, 105: 336, 106: 336, 107: 336, 108: 336, 109: 336, 110: 336, 111: 336, 112: 336, 113: 336, 114: 336, 115: 336, 116: 336, 117: 336, 118: 336, 119: 336, 120: 336, 121: 336, 122: 336, 123: 336, 124: 336, 125: 336, 126: 336, 127: 336, 128: 336, 129: 336, 130: 336, 131: 336, 132: 336, 133: 336, 134: 336, 135: 336, 136: 336, 137: 336, 138: 336, 139: 336, 140: 336, 141: 336, 142: 336, 143: 336, 144: 336, 145: 336, 146: 336, 147: 336, 148: 336, 149: 336, 150: 336, 151: 336, 152: 336, 153: 336, 154: 336, 155: 336, 156: 336, 157: 336, 158: 336, 159: 336, 160: 336, 161: 336, 162: 336, 163: 336, 164: 336, 165: 336, 166: 336, 167: 336, 168: 336, 169: 336, 170: 336, 171: 336, 172: 336, 173: 336, 174: 336, 175: 336, 176: 336, 177: 336, 178: 336, 179: 336, 180: 336, 181: 336, 182: 336, 183: 336, 184: 336, 185: 336, 186: 336, 187: 336, 188: 336, 189: 336, 190: 336, 191: 336, 192: 336, 193: 336, 194: 336, 195: 336, 196: 336, 197: 336, 198: 336, 199: 336, 200: 336, 201: 336, 202: 336, 203: 336, 204: 336, 205: 336, 206: 336, 207: 336, 208: 336, 209: 336, 210: 336, 211: 336, 212: 336, 213: 336, 214: 336, 215: 336, 216: 336, 217: 336, 218: 336, 219: 336, 220: 336, 221: 336, 222: 336, 223: 336, 224: 336, 225: 336, 226: 336, 227: 336, 228: 336, 229: 336, 230: 336, 231: 336, 232: 336, 233: 336, 234: 336, 235: 336, 236: 336, 237: 336, 238: 336, 239: 336, 240: 336, 241: 336, 242: 336, 243: 336, 244: 336, 245: 336, 246: 336, 247: 336, 248: 336, 249: 336, 250: 336, 251: 336, 252: 336, 253: 336, 254: 336}
The normalized cdf values are {0: 80, 1: 190, 2: 331, 3: 501, 4: 667, 5: 789, 6: 914, 7: 1092, 8: 1206, 9: 1355, 10: 1355, 11: 1355, 12: 1355, 13: 1355, 14: 1355, 15: 1355, 16: 1355, 17: 1355, 18: 1355, 19: 1355, 20: 1355, 21: 1355, 22: 1355, 23: 1355, 24: 1355, 25: 1355, 26: 1355, 27: 1355, 28: 1355, 29: 1355, 30: 1355, 31: 1355, 32: 1355, 33: 1355, 34: 1355, 35: 1355, 36: 1355, 37: 1355, 38: 1355, 39: 1355, 40: 1355, 41: 1355, 42: 1355, 43: 1355, 44: 1355, 45: 1355, 46: 1355, 47: 1355, 48: 1355, 49: 1355, 50: 1355, 51: 1355, 52: 1355, 53: 1355, 54: 1355, 55: 1355, 56: 1355, 57: 1355, 58: 1355, 59: 1355, 60: 1355, 61: 1355, 62: 1355, 63: 1355, 64: 1355, 65: 1355, 66: 1355, 67: 1355, 68: 1355, 69: 1355, 70: 1355, 71: 1355, 72: 1355, 73: 1355, 74: 1355, 75: 1355, 76: 1355, 77: 1355, 78: 1355, 79: 1355, 80: 1355, 81: 1355, 82: 1355, 83: 1355, 84: 1355, 85: 1355, 86: 1355, 87: 1355, 88: 1355, 89: 1355, 90: 1355, 91: 1355, 92: 1355, 93: 1355, 94: 1355, 95: 1355, 96: 1355, 97: 1355, 98: 1355, 99: 1355, 100: 1355, 101: 1355, 102: 1355, 103: 1355, 104: 1355, 105: 1355, 106: 1355, 107: 1355, 108: 1355, 109: 1355, 110: 1355, 111: 1355, 112: 1355, 113: 1355, 114: 1355, 115: 1355, 116: 1355, 117: 1355, 118: 1355, 119: 1355, 120: 1355, 121: 1355, 122: 1355, 123: 1355, 124: 1355, 125: 1355, 126: 1355, 127: 1355, 128: 1355, 129: 1355, 130: 1355, 131: 1355, 132: 1355, 133: 1355, 134: 1355, 135: 1355, 136: 1355, 137: 1355, 138: 1355, 139: 1355, 140: 1355, 141: 1355, 142: 1355, 143: 1355, 144: 1355, 145: 1355, 146: 1355, 147: 1355, 148: 1355, 149: 1355, 150: 1355, 151: 1355, 152: 1355, 153: 1355, 154: 1355, 155: 1355, 156: 1355, 157: 1355, 158: 1355, 159: 1355, 160: 1355, 161: 1355, 162: 1355, 163: 1355, 164: 1355, 165: 1355, 166: 1355, 167: 1355, 168: 1355, 169: 1355, 170: 1355, 171: 1355, 172: 1355, 173: 1355, 174: 1355, 175: 1355, 176: 1355, 177: 1355, 178: 1355, 179: 1355, 180: 1355, 181: 1355, 182: 1355, 183: 1355, 184: 1355, 185: 1355, 186: 1355, 187: 1355, 188: 1355, 189: 1355, 190: 1355, 191: 1355, 192: 1355, 193: 1355, 194: 1355, 195: 1355, 196: 1355, 197: 1355, 198: 1355, 199: 1355, 200: 1355, 201: 1355, 202: 1355, 203: 1355, 204: 1355, 205: 1355, 206: 1355, 207: 1355, 208: 1355, 209: 1355, 210: 1355, 211: 1355, 212: 1355, 213: 1355, 214: 1355, 215: 1355, 216: 1355, 217: 1355, 218: 1355, 219: 1355, 220: 1355, 221: 1355, 222: 1355, 223: 1355, 224: 1355, 225: 1355, 226: 1355, 227: 1355, 228: 1355, 229: 1355, 230: 1355, 231: 1355, 232: 1355, 233: 1355, 234: 1355, 235: 1355, 236: 1355, 237: 1355, 238: 1355, 239: 1355, 240: 1355, 241: 1355, 242: 1355, 243: 1355, 244: 1355, 245: 1355, 246: 1355, 247: 1355, 248: 1355, 249: 1355, 250: 1355, 251: 1355, 252: 1355, 253: 1355, 254: 1355}

In [29]:
newimg = np.empty(img.shape)
## This should be the same exact dimensions as the original array

print newimg 
print img.shape

## set x_length to the first number, y_length to the second
x_length = img.shape[0]
y_length = img.shape[1]
z_length = img.shape[2]
print ''
print "XYZ lengths: "
print x_length, y_length, z_length


for i in range(x_length):
    for j in range(y_length):
        for k in range(z_length):
            newimg[i][j][k] = norm_cdf[ img[i][j][k] ]

print newimg

TwoDimg = img.reshape(-1)

fig = plt.hist(TwoDimg, bins = 'auto')
plt.title('Equalized Histogram')
plt.show



## This is wrong


[[[  2.00000000e+000   2.00000000e+000   2.12199579e-314   0.00000000e+000
     4.94065646e-324   0.00000000e+000   0.00000000e+000   0.00000000e+000]
  [ -8.89476941e+205   2.24783265e-314   2.21388946e-314   8.17079486e+097
     2.24883445e-314   2.21388946e-314   0.00000000e+000   0.00000000e+000]
  [  0.00000000e+000   1.23920719e-149   2.24794985e-314   2.21388946e-314
     0.00000000e+000   0.00000000e+000   0.00000000e+000  -1.69506615e+010]
  [  2.24783256e-314   2.21385056e-314   0.00000000e+000   0.00000000e+000
     0.00000000e+000   3.15023826e+144   2.24884762e-314   2.21385057e-314]
  [  0.00000000e+000   0.00000000e+000   0.00000000e+000   1.06615507e-217
     2.24886428e-314   2.21388946e-314   0.00000000e+000   0.00000000e+000]
  [  0.00000000e+000   0.00000000e+000   0.00000000e+000   0.00000000e+000
     8.77805240e+030   2.24885818e-314   6.95079640e-310   4.13957608e-098]
  [  2.24783253e-314   2.21388946e-314   7.56560098e-308   2.24775350e-314
     2.26914780e-314   7.56560098e-308   2.21994435e-314   2.26919653e-314]]

 [[  0.00000000e+000   0.00000000e+000   0.00000000e+000   2.54693524e+166
     2.24783260e-314   2.21388946e-314   0.00000000e+000   0.00000000e+000]
  [  0.00000000e+000   0.00000000e+000   0.00000000e+000   0.00000000e+000
     0.00000000e+000   0.00000000e+000   0.00000000e+000   9.21075693e+255]
  [  2.24783263e-314   2.21388946e-314  -1.51632664e-224   2.24883473e-314
     2.25260391e-314   0.00000000e+000   0.00000000e+000   0.00000000e+000]
  [  1.16578090e+003   2.24783267e-314   2.21385057e-314   0.00000000e+000
     0.00000000e+000   0.00000000e+000   0.00000000e+000   0.00000000e+000]
  [  0.00000000e+000   2.82975255e-308   2.24883390e-314   2.21388946e-314
     0.00000000e+000   0.00000000e+000   0.00000000e+000  -1.18910280e+080]
  [  2.24783241e-314   2.21388946e-314   0.00000000e+000   0.00000000e+000
     0.00000000e+000  -5.55337610e+190   2.24784126e-314   2.27097661e-314]
  [  0.00000000e+000   0.00000000e+000   0.00000000e+000   9.23800141e-068
     2.24884734e-314   2.25260394e-314  -6.43109926e+222   2.24885860e-314]]

 [[  2.21388946e-314   2.89723529e+109   2.24883459e-314   6.95079636e-310
     2.68794292e+023   2.24783270e-314   2.21385057e-314   0.00000000e+000]
  [  0.00000000e+000   0.00000000e+000   0.00000000e+000   0.00000000e+000
     0.00000000e+000  -2.28688795e-036   2.24784129e-314   2.21388946e-314]
  [  0.00000000e+000   0.00000000e+000   0.00000000e+000  -9.88184156e+245
     4.94065646e-324   6.95079640e-310  -4.29634706e-054   2.24883367e-314]
  [  2.26939577e-314   7.69810654e+178   2.24885810e-314   2.24891328e-314
     7.92630105e+265   2.24878962e-314   2.22984797e-314   1.40105902e+180]
  [  2.24783275e-314   2.21388946e-314   0.00000000e+000   0.00000000e+000
     0.00000000e+000   0.00000000e+000   0.00000000e+000   0.00000000e+000]
  [  0.00000000e+000   0.00000000e+000   0.00000000e+000   0.00000000e+000
     0.00000000e+000   0.00000000e+000  -1.10986412e-109   2.24783258e-314]
  [  2.21388946e-314  -5.17740075e+079   2.24885843e-314   2.24887736e-314
     0.00000000e+000   2.27108253e-314   6.95079831e-310   4.94065646e-324]]

 [[  0.00000000e+000   0.00000000e+000   1.55936428e+015   2.24808549e-314
     2.21385056e-314   4.46658229e-116   2.24885804e-314   2.24891311e-314]
  [  0.00000000e+000   0.00000000e+000   0.00000000e+000   0.00000000e+000
     0.00000000e+000   0.00000000e+000   6.55100668e-240   2.24783279e-314]
  [  2.21388946e-314   0.00000000e+000   0.00000000e+000   0.00000000e+000
    -5.90031045e+021   2.24783239e-314   2.21385056e-314   5.34437903e-061]
  [  2.24871035e-314   2.26919693e-314   0.00000000e+000   0.00000000e+000
     0.00000000e+000   0.00000000e+000   0.00000000e+000   0.00000000e+000]
  [  0.00000000e+000   0.00000000e+000   0.00000000e+000   0.00000000e+000
     0.00000000e+000   0.00000000e+000   4.92396524e+038   2.24849997e-314]
  [  2.23321843e-314   0.00000000e+000   0.00000000e+000   0.00000000e+000
     6.13527392e+034   2.24783282e-314   2.21388946e-314   1.40336253e+180]
  [  2.24783272e-314   6.95079636e-310  -1.10908200e-179   2.24883341e-314
     2.24883326e-314  -3.76726571e+227   2.24878985e-314   2.22984807e-314]]

 [[  0.00000000e+000   0.00000000e+000   0.00000000e+000   0.00000000e+000
     0.00000000e+000   0.00000000e+000   0.00000000e+000   0.00000000e+000]
  [  0.00000000e+000   0.00000000e+000   0.00000000e+000   0.00000000e+000
     0.00000000e+000   0.00000000e+000   0.00000000e+000   0.00000000e+000]
  [  0.00000000e+000   0.00000000e+000   4.21189912e+266   2.24784124e-314
     2.21385056e-314   2.31767342e-236   2.24783248e-314   6.95079640e-310]
  [ -3.65265909e+292   2.24878946e-314   2.22984797e-314   6.18551292e-226
     2.24883431e-314   6.95079859e-310   0.00000000e+000   0.00000000e+000]
  [  0.00000000e+000   5.69074804e+023   2.24783244e-314   2.21385056e-314
     0.00000000e+000   0.00000000e+000   0.00000000e+000   0.00000000e+000]
  [  0.00000000e+000   0.00000000e+000   0.00000000e+000   0.00000000e+000
     0.00000000e+000   0.00000000e+000   0.00000000e+000   0.00000000e+000]
  [  0.00000000e+000   0.00000000e+000   0.00000000e+000   3.15023826e+144
     2.24884779e-314   2.21385057e-314   7.03908167e-199   2.24883355e-314]]

 [[  2.23321107e-314   0.00000000e+000   0.00000000e+000   0.00000000e+000
     0.00000000e+000   0.00000000e+000   0.00000000e+000   1.40520528e+180]
  [  2.24783277e-314   2.21388946e-314  -5.73273277e-124   2.24783251e-314
     2.21388946e-314   3.04075076e-291   2.24791453e-314   6.95079636e-310]
  [  0.00000000e+000   0.00000000e+000   0.00000000e+000   0.00000000e+000
     0.00000000e+000   0.00000000e+000  -4.82690518e+283   2.24884786e-314]
  [  2.21385057e-314   0.00000000e+000   0.00000000e+000   0.00000000e+000
     0.00000000e+000   0.00000000e+000   0.00000000e+000   0.00000000e+000]
  [  0.00000000e+000   0.00000000e+000   0.00000000e+000   0.00000000e+000
     0.00000000e+000   9.19332133e-307   2.24797231e-314   2.26939634e-314]
  [  2.00000000e+000   2.00000000e+000   0.00000000e+000  -2.91280468e+268
     2.24779147e-314   2.21554726e-314   0.00000000e+000   0.00000000e+000]
  [  2.23029957e-314   8.32399253e-200   2.21676497e-314   2.26939631e-314
     0.00000000e+000   0.00000000e+000   0.00000000e+000  -1.07386727e-293]]]
(6, 7, 8)

XYZ lengths: 
6 7 8
[[[ 1206.   331.  1092.   501.   501.  1206.    80.   190.]
  [ 1355.   667.  1355.   914.  1092.   331.   190.  1206.]
  [ 1092.  1092.   331.  1092.   667.   914.   190.   789.]
  [  667.   667.  1092.   667.   914.   667.  1355.   331.]
  [  667.  1206.  1206.   914.  1092.   501.   667.    80.]
  [  914.  1092.   501.   331.   667.   501.  1092.   501.]
  [  667.   789.   667.   501.    80.   501.   667.  1355.]]

 [[  914.  1092.   667.  1206.  1355.    80.   914.   789.]
  [  667.  1355.   914.  1092.   789.   501.   789.  1206.]
  [ 1355.   914.   501.  1092.  1092.   501.   501.   501.]
  [  501.   789.    80.   789.  1355.   501.  1092.   501.]
  [  667.  1206.   331.  1355.   914.   501.   331.   667.]
  [  501.   331.   914.   190.   501.  1092.   190.   789.]
  [ 1206.  1206.   501.    80.   501.  1092.   331.   667.]]

 [[  914.   190.   789.   331.  1092.  1092.   667.   501.]
  [  331.    80.    80.  1092.   914.  1206.   667.  1092.]
  [  501.   667.   190.  1206.  1092.   190.   190.   789.]
  [ 1206.  1355.   501.   190.  1092.   190.   914.   789.]
  [  331.   501.   331.   667.  1206.  1092.  1092.  1206.]
  [  331.  1206.    80.   789.  1355.   789.   914.    80.]
  [  331.    80.   190.  1355.  1092.   190.  1092.  1355.]]

 [[ 1355.   914.  1206.    80.  1206.   331.  1092.  1355.]
  [  501.  1355.    80.  1092.   914.   789.   501.   501.]
  [   80.  1092.   667.  1092.   667.  1206.   331.   331.]
  [  667.  1206.    80.   789.   190.  1206.  1092.   789.]
  [ 1355.   190.   789.   190.   331.   331.   789.  1355.]
  [  914.   789.   501.   667.   331.   331.   789.    80.]
  [  501.   789.   667.   190.    80.   914.   667.   914.]]

 [[ 1092.   331.   331.   331.   190.   190.  1355.   667.]
  [ 1355.  1206.  1355.   789.  1355.  1355.   789.   501.]
  [  914.   914.   501.    80.   501.  1206.   501.   501.]
  [   80.   914.  1092.   501.  1355.   667.   331.   667.]
  [  190.   190.   501.   501.   331.   331.   667.   331.]
  [ 1355.   789.  1092.   501.   331.   667.   501.  1355.]
  [  190.   667.   914.   331.  1092.   331.  1355.  1355.]]

 [[  667.   190.  1355.   331.  1206.  1092.  1206.  1206.]
  [  914.  1355.   331.  1206.   914.  1206.  1092.   667.]
  [  914.   667.  1092.   789.   667.   789.   190.   667.]
  [  667.   914.    80.  1092.   190.  1092.  1355.   789.]
  [ 1355.   914.  1355.   914.   190.   501.  1092.   914.]
  [  331.   190.  1092.  1092.   789.    80.  1092.  1355.]
  [ 1355.   789.   667.  1355.   667.   789.  1355.   501.]]]
Out[29]:
<function matplotlib.pyplot.show>

In [30]:
print ''
print 'Original subimage:'
print ''
imgflat = img.reshape(-1)
print img

print " "
fig = plt.hist(imgflat, bins='auto')
plt.title('Original Histogram')
plt.show()

print ''
print ''
print 'Equalized subimage:'
print ''

print ''
print newimg

TwoDimg = newimg.reshape(-1)

fig = plt.hist(TwoDimg, bins = 'auto')
plt.title('Equalized Histogram')
plt.show


Original subimage:

[[[8 2 7 3 3 8 0 1]
  [9 4 9 6 7 2 1 8]
  [7 7 2 7 4 6 1 5]
  [4 4 7 4 6 4 9 2]
  [4 8 8 6 7 3 4 0]
  [6 7 3 2 4 3 7 3]
  [4 5 4 3 0 3 4 9]]

 [[6 7 4 8 9 0 6 5]
  [4 9 6 7 5 3 5 8]
  [9 6 3 7 7 3 3 3]
  [3 5 0 5 9 3 7 3]
  [4 8 2 9 6 3 2 4]
  [3 2 6 1 3 7 1 5]
  [8 8 3 0 3 7 2 4]]

 [[6 1 5 2 7 7 4 3]
  [2 0 0 7 6 8 4 7]
  [3 4 1 8 7 1 1 5]
  [8 9 3 1 7 1 6 5]
  [2 3 2 4 8 7 7 8]
  [2 8 0 5 9 5 6 0]
  [2 0 1 9 7 1 7 9]]

 [[9 6 8 0 8 2 7 9]
  [3 9 0 7 6 5 3 3]
  [0 7 4 7 4 8 2 2]
  [4 8 0 5 1 8 7 5]
  [9 1 5 1 2 2 5 9]
  [6 5 3 4 2 2 5 0]
  [3 5 4 1 0 6 4 6]]

 [[7 2 2 2 1 1 9 4]
  [9 8 9 5 9 9 5 3]
  [6 6 3 0 3 8 3 3]
  [0 6 7 3 9 4 2 4]
  [1 1 3 3 2 2 4 2]
  [9 5 7 3 2 4 3 9]
  [1 4 6 2 7 2 9 9]]

 [[4 1 9 2 8 7 8 8]
  [6 9 2 8 6 8 7 4]
  [6 4 7 5 4 5 1 4]
  [4 6 0 7 1 7 9 5]
  [9 6 9 6 1 3 7 6]
  [2 1 7 7 5 0 7 9]
  [9 5 4 9 4 5 9 3]]]
 

Equalized subimage:


[[[ 1206.   331.  1092.   501.   501.  1206.    80.   190.]
  [ 1355.   667.  1355.   914.  1092.   331.   190.  1206.]
  [ 1092.  1092.   331.  1092.   667.   914.   190.   789.]
  [  667.   667.  1092.   667.   914.   667.  1355.   331.]
  [  667.  1206.  1206.   914.  1092.   501.   667.    80.]
  [  914.  1092.   501.   331.   667.   501.  1092.   501.]
  [  667.   789.   667.   501.    80.   501.   667.  1355.]]

 [[  914.  1092.   667.  1206.  1355.    80.   914.   789.]
  [  667.  1355.   914.  1092.   789.   501.   789.  1206.]
  [ 1355.   914.   501.  1092.  1092.   501.   501.   501.]
  [  501.   789.    80.   789.  1355.   501.  1092.   501.]
  [  667.  1206.   331.  1355.   914.   501.   331.   667.]
  [  501.   331.   914.   190.   501.  1092.   190.   789.]
  [ 1206.  1206.   501.    80.   501.  1092.   331.   667.]]

 [[  914.   190.   789.   331.  1092.  1092.   667.   501.]
  [  331.    80.    80.  1092.   914.  1206.   667.  1092.]
  [  501.   667.   190.  1206.  1092.   190.   190.   789.]
  [ 1206.  1355.   501.   190.  1092.   190.   914.   789.]
  [  331.   501.   331.   667.  1206.  1092.  1092.  1206.]
  [  331.  1206.    80.   789.  1355.   789.   914.    80.]
  [  331.    80.   190.  1355.  1092.   190.  1092.  1355.]]

 [[ 1355.   914.  1206.    80.  1206.   331.  1092.  1355.]
  [  501.  1355.    80.  1092.   914.   789.   501.   501.]
  [   80.  1092.   667.  1092.   667.  1206.   331.   331.]
  [  667.  1206.    80.   789.   190.  1206.  1092.   789.]
  [ 1355.   190.   789.   190.   331.   331.   789.  1355.]
  [  914.   789.   501.   667.   331.   331.   789.    80.]
  [  501.   789.   667.   190.    80.   914.   667.   914.]]

 [[ 1092.   331.   331.   331.   190.   190.  1355.   667.]
  [ 1355.  1206.  1355.   789.  1355.  1355.   789.   501.]
  [  914.   914.   501.    80.   501.  1206.   501.   501.]
  [   80.   914.  1092.   501.  1355.   667.   331.   667.]
  [  190.   190.   501.   501.   331.   331.   667.   331.]
  [ 1355.   789.  1092.   501.   331.   667.   501.  1355.]
  [  190.   667.   914.   331.  1092.   331.  1355.  1355.]]

 [[  667.   190.  1355.   331.  1206.  1092.  1206.  1206.]
  [  914.  1355.   331.  1206.   914.  1206.  1092.   667.]
  [  914.   667.  1092.   789.   667.   789.   190.   667.]
  [  667.   914.    80.  1092.   190.  1092.  1355.   789.]
  [ 1355.   914.  1355.   914.   190.   501.  1092.   914.]
  [  331.   190.  1092.  1092.   789.    80.  1092.  1355.]
  [ 1355.   789.   667.  1355.   667.   789.  1355.   501.]]]
Out[30]:
<function matplotlib.pyplot.show>

In [31]:
fig = plt.hist(imgflat, bins='auto')
plt.title('Original Histogram')
plt.show()

fig = plt.hist(TwoDimg, bins = 'auto')
plt.title('Equalized Histogram')
plt.show()



In [32]:
fig = plt.hist(imgflat, bins=255)
plt.title('Original Histogram')
plt.show()

fig = plt.hist(TwoDimg, bins = 255)
plt.title('Equalized Histogram')
plt.show()



In [33]:
print img
print ''
print newimg
print ''

flattenedimg, bin_edges1 = np.histogram(imgflat)
print imgflat
print flattenedimg
print ''

flattenednewimg, bin_edges2 = np.histogram(TwoDimg)
print TwoDimg
print flattenednewimg
print ''


[[[8 2 7 3 3 8 0 1]
  [9 4 9 6 7 2 1 8]
  [7 7 2 7 4 6 1 5]
  [4 4 7 4 6 4 9 2]
  [4 8 8 6 7 3 4 0]
  [6 7 3 2 4 3 7 3]
  [4 5 4 3 0 3 4 9]]

 [[6 7 4 8 9 0 6 5]
  [4 9 6 7 5 3 5 8]
  [9 6 3 7 7 3 3 3]
  [3 5 0 5 9 3 7 3]
  [4 8 2 9 6 3 2 4]
  [3 2 6 1 3 7 1 5]
  [8 8 3 0 3 7 2 4]]

 [[6 1 5 2 7 7 4 3]
  [2 0 0 7 6 8 4 7]
  [3 4 1 8 7 1 1 5]
  [8 9 3 1 7 1 6 5]
  [2 3 2 4 8 7 7 8]
  [2 8 0 5 9 5 6 0]
  [2 0 1 9 7 1 7 9]]

 [[9 6 8 0 8 2 7 9]
  [3 9 0 7 6 5 3 3]
  [0 7 4 7 4 8 2 2]
  [4 8 0 5 1 8 7 5]
  [9 1 5 1 2 2 5 9]
  [6 5 3 4 2 2 5 0]
  [3 5 4 1 0 6 4 6]]

 [[7 2 2 2 1 1 9 4]
  [9 8 9 5 9 9 5 3]
  [6 6 3 0 3 8 3 3]
  [0 6 7 3 9 4 2 4]
  [1 1 3 3 2 2 4 2]
  [9 5 7 3 2 4 3 9]
  [1 4 6 2 7 2 9 9]]

 [[4 1 9 2 8 7 8 8]
  [6 9 2 8 6 8 7 4]
  [6 4 7 5 4 5 1 4]
  [4 6 0 7 1 7 9 5]
  [9 6 9 6 1 3 7 6]
  [2 1 7 7 5 0 7 9]
  [9 5 4 9 4 5 9 3]]]

[[[ 1206.   331.  1092.   501.   501.  1206.    80.   190.]
  [ 1355.   667.  1355.   914.  1092.   331.   190.  1206.]
  [ 1092.  1092.   331.  1092.   667.   914.   190.   789.]
  [  667.   667.  1092.   667.   914.   667.  1355.   331.]
  [  667.  1206.  1206.   914.  1092.   501.   667.    80.]
  [  914.  1092.   501.   331.   667.   501.  1092.   501.]
  [  667.   789.   667.   501.    80.   501.   667.  1355.]]

 [[  914.  1092.   667.  1206.  1355.    80.   914.   789.]
  [  667.  1355.   914.  1092.   789.   501.   789.  1206.]
  [ 1355.   914.   501.  1092.  1092.   501.   501.   501.]
  [  501.   789.    80.   789.  1355.   501.  1092.   501.]
  [  667.  1206.   331.  1355.   914.   501.   331.   667.]
  [  501.   331.   914.   190.   501.  1092.   190.   789.]
  [ 1206.  1206.   501.    80.   501.  1092.   331.   667.]]

 [[  914.   190.   789.   331.  1092.  1092.   667.   501.]
  [  331.    80.    80.  1092.   914.  1206.   667.  1092.]
  [  501.   667.   190.  1206.  1092.   190.   190.   789.]
  [ 1206.  1355.   501.   190.  1092.   190.   914.   789.]
  [  331.   501.   331.   667.  1206.  1092.  1092.  1206.]
  [  331.  1206.    80.   789.  1355.   789.   914.    80.]
  [  331.    80.   190.  1355.  1092.   190.  1092.  1355.]]

 [[ 1355.   914.  1206.    80.  1206.   331.  1092.  1355.]
  [  501.  1355.    80.  1092.   914.   789.   501.   501.]
  [   80.  1092.   667.  1092.   667.  1206.   331.   331.]
  [  667.  1206.    80.   789.   190.  1206.  1092.   789.]
  [ 1355.   190.   789.   190.   331.   331.   789.  1355.]
  [  914.   789.   501.   667.   331.   331.   789.    80.]
  [  501.   789.   667.   190.    80.   914.   667.   914.]]

 [[ 1092.   331.   331.   331.   190.   190.  1355.   667.]
  [ 1355.  1206.  1355.   789.  1355.  1355.   789.   501.]
  [  914.   914.   501.    80.   501.  1206.   501.   501.]
  [   80.   914.  1092.   501.  1355.   667.   331.   667.]
  [  190.   190.   501.   501.   331.   331.   667.   331.]
  [ 1355.   789.  1092.   501.   331.   667.   501.  1355.]
  [  190.   667.   914.   331.  1092.   331.  1355.  1355.]]

 [[  667.   190.  1355.   331.  1206.  1092.  1206.  1206.]
  [  914.  1355.   331.  1206.   914.  1206.  1092.   667.]
  [  914.   667.  1092.   789.   667.   789.   190.   667.]
  [  667.   914.    80.  1092.   190.  1092.  1355.   789.]
  [ 1355.   914.  1355.   914.   190.   501.  1092.   914.]
  [  331.   190.  1092.  1092.   789.    80.  1092.  1355.]
  [ 1355.   789.   667.  1355.   667.   789.  1355.   501.]]]

[8 2 7 3 3 8 0 1 9 4 9 6 7 2 1 8 7 7 2 7 4 6 1 5 4 4 7 4 6 4 9 2 4 8 8 6 7
 3 4 0 6 7 3 2 4 3 7 3 4 5 4 3 0 3 4 9 6 7 4 8 9 0 6 5 4 9 6 7 5 3 5 8 9 6
 3 7 7 3 3 3 3 5 0 5 9 3 7 3 4 8 2 9 6 3 2 4 3 2 6 1 3 7 1 5 8 8 3 0 3 7 2
 4 6 1 5 2 7 7 4 3 2 0 0 7 6 8 4 7 3 4 1 8 7 1 1 5 8 9 3 1 7 1 6 5 2 3 2 4
 8 7 7 8 2 8 0 5 9 5 6 0 2 0 1 9 7 1 7 9 9 6 8 0 8 2 7 9 3 9 0 7 6 5 3 3 0
 7 4 7 4 8 2 2 4 8 0 5 1 8 7 5 9 1 5 1 2 2 5 9 6 5 3 4 2 2 5 0 3 5 4 1 0 6
 4 6 7 2 2 2 1 1 9 4 9 8 9 5 9 9 5 3 6 6 3 0 3 8 3 3 0 6 7 3 9 4 2 4 1 1 3
 3 2 2 4 2 9 5 7 3 2 4 3 9 1 4 6 2 7 2 9 9 4 1 9 2 8 7 8 8 6 9 2 8 6 8 7 4
 6 4 7 5 4 5 1 4 4 6 0 7 1 7 9 5 9 6 9 6 1 3 7 6 2 1 7 7 5 0 7 9 9 5 4 9 4
 5 9 3]
[21 27 35 42 41 30 31 44 28 37]

[ 1206.   331.  1092.   501.   501.  1206.    80.   190.  1355.   667.
  1355.   914.  1092.   331.   190.  1206.  1092.  1092.   331.  1092.
   667.   914.   190.   789.   667.   667.  1092.   667.   914.   667.
  1355.   331.   667.  1206.  1206.   914.  1092.   501.   667.    80.
   914.  1092.   501.   331.   667.   501.  1092.   501.   667.   789.
   667.   501.    80.   501.   667.  1355.   914.  1092.   667.  1206.
  1355.    80.   914.   789.   667.  1355.   914.  1092.   789.   501.
   789.  1206.  1355.   914.   501.  1092.  1092.   501.   501.   501.
   501.   789.    80.   789.  1355.   501.  1092.   501.   667.  1206.
   331.  1355.   914.   501.   331.   667.   501.   331.   914.   190.
   501.  1092.   190.   789.  1206.  1206.   501.    80.   501.  1092.
   331.   667.   914.   190.   789.   331.  1092.  1092.   667.   501.
   331.    80.    80.  1092.   914.  1206.   667.  1092.   501.   667.
   190.  1206.  1092.   190.   190.   789.  1206.  1355.   501.   190.
  1092.   190.   914.   789.   331.   501.   331.   667.  1206.  1092.
  1092.  1206.   331.  1206.    80.   789.  1355.   789.   914.    80.
   331.    80.   190.  1355.  1092.   190.  1092.  1355.  1355.   914.
  1206.    80.  1206.   331.  1092.  1355.   501.  1355.    80.  1092.
   914.   789.   501.   501.    80.  1092.   667.  1092.   667.  1206.
   331.   331.   667.  1206.    80.   789.   190.  1206.  1092.   789.
  1355.   190.   789.   190.   331.   331.   789.  1355.   914.   789.
   501.   667.   331.   331.   789.    80.   501.   789.   667.   190.
    80.   914.   667.   914.  1092.   331.   331.   331.   190.   190.
  1355.   667.  1355.  1206.  1355.   789.  1355.  1355.   789.   501.
   914.   914.   501.    80.   501.  1206.   501.   501.    80.   914.
  1092.   501.  1355.   667.   331.   667.   190.   190.   501.   501.
   331.   331.   667.   331.  1355.   789.  1092.   501.   331.   667.
   501.  1355.   190.   667.   914.   331.  1092.   331.  1355.  1355.
   667.   190.  1355.   331.  1206.  1092.  1206.  1206.   914.  1355.
   331.  1206.   914.  1206.  1092.   667.   914.   667.  1092.   789.
   667.   789.   190.   667.   667.   914.    80.  1092.   190.  1092.
  1355.   789.  1355.   914.  1355.   914.   190.   501.  1092.   914.
   331.   190.  1092.  1092.   789.    80.  1092.  1355.  1355.   789.
   667.  1355.   667.   789.  1355.   501.]
[48 35  0 42 41 30 31 44 28 37]


In [34]:
fig = plt.hist(flattenedimg, bins = 255)
plt.title('Original Histogram (Flat version)')
plt.show


Out[34]:
<function matplotlib.pyplot.show>

In [35]:
fig = plt.hist(flattenednewimg, bins = 255)
plt.title('Equalized Histogram (Flat version)')
plt.show


Out[35]:
<function matplotlib.pyplot.show>

In [ ]: